package com.dounine.clouddisk360.parser.deserializer.passport;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;

import com.dounine.clouddisk360.parser.PassportParser;

public class PassportResponseHandle implements ResponseHandler<Passport> {
	
	private PassportParser passportParser;
	public PassportResponseHandle(PassportParser passportParser) {
		this.passportParser = passportParser;
	}

	@Override
	public Passport handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
		HttpEntity entity = response.getEntity();
		saveImgToPath(entity.getContent());
		return null;
	}
	
	public void saveImgToPath(InputStream is) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(new File(passportParser.getConfig().getImgSavePath() + "验证码.jpg"));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		byte[] data = new byte[1024 * 1024];
		int length = 0;
		try {
			while ((length = is.read(data)) != -1) {
				os.write(data, 0, length);
			}
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
